package 单周赛.history;

import util.TreeNode;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

/**
 *
 */
public class 第335场单周赛 {

    public static void main(String[] args) {

        System.out.println(passThePillow(18, 38));

    }

    public static int passThePillow(int n, int time) {
        int curN = 1;
        boolean add = true;
        while (time-- > 0) {
            if (add) {
                curN++;
            } else {
                curN--;
            }
            if (curN == n) {
                add = false;
            }
            if (curN == 1) {
                add = true;
            }
        }
        return curN;
    }

    public long kthLargestLevelSum(TreeNode root, int k) {
        List<Long> list = new ArrayList<>();
        Deque<TreeNode> queue = new ArrayDeque<>();
        queue.addLast(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            long curSum = 0;
            while (size-- > 0) {
                TreeNode curNode = queue.pollFirst();
                curSum += curNode.val;
                if (curNode.left != null) {
                    queue.addLast(curNode.left);
                }
                if (curNode.right != null) {
                    queue.addLast(curNode.right);
                }
            }
            list.add(curSum);
        }
        list.sort((a, b) -> b > a ? 1 : -1);
        return (k - 1) < list.size() ? list.get(k - 1) : -1;
    }

}
